package utils

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"github.com/golang-jwt/jwt"
	"github.com/kataras/iris/v12/context"
	"time"
)

func GetTokenString(template *domain.User) (string, error) {
	token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
		"username": template.Username,
		"id":       template.Id,
		"exp":      time.Now().Unix() + common.JwtTime,
	})
	return token.SignedString([]byte(common.JwtKey))
}

func GetJwtParameter(context *context.Context, value string) interface{} {
	header := context.GetHeader("Authorization")
	token, _ := jwt.Parse(header, nil)
	claims := token.Claims
	return claims.(jwt.MapClaims)[value]
}
